package com.zhonglz.util.date;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * 获取yyyy-MM-dd格式的当前时间
	 * @return 当前时间
	 */
	public static String getDate(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	    String result = format.format(new Date());  
		return result;
	}
	
	
	/****
	 * String转Date格式返回
	 * @param dateStr
	 * @return
	 */
	public static Date dateByString(String dateStr) {  
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
	    Date certNotAfter = null;
		try {
			certNotAfter = simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    return certNotAfter;  
	}
	
	/** 
    * 获取过去第几天的日期 
    * 
    * @param past 
    * @return 
    */  
   public static String getPastDate(int past) {  
       Calendar calendar = Calendar.getInstance();
       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
       Date today = calendar.getTime();  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String result = format.format(today);  
       return result;  
   }

	/**
    * 获取未来第几天的日期 
    * 
    * @param past 
    * @return 
    */  
   public static String getLastDate(int last) {  
       Calendar calendar = Calendar.getInstance();  
       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + last);  
       Date today = calendar.getTime();  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
       String result = format.format(today);  
       return result;  
   } 
   
   
   
   
	   
    /**
     * 返回两个时间段的相隔天数
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
	public static int daysBetween(String smdate, String bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days))+1;
	}



	/**
	 * 获取过去第几天的日期
	 *
	 * @param past
	 * @return
	 */
	public static String getPastDates(String dateStr, int past){
		// 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
		Date date = sdf.parse(dateStr, new ParsePosition(0));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
		calendar.add(Calendar.DATE, -past);
		Date date1 = calendar.getTime();
		String result = sdf.format(date1);
		return result;
	}
    public static void main(String[] args) throws ParseException {
    	/*System.out.println(getPastDate(6));
    	System.out.println(getPastDate(7));
    	System.out.println(getPastDate(13));
    	System.out.println(getPastDate(15));*/

		System.out.println(getPastDates("2021-01-20",1));
	}

}
